package com.fds;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.widget.ImageView;

public class MyImageView extends ImageView {
	
    private float x=-1;
    private float y=-1;
    

	public MyImageView(Context context) {
		super(context);
	}
	
	public MyImageView(Context context, AttributeSet attrs)
	{
	        super(context, attrs);
	}
	
	public MyImageView(Context context, AttributeSet attrs, int defStyle)
	{
	        super(context, attrs, defStyle);
	}	
	
	@Override
	protected void onDraw(Canvas canvas) {
		
		super.onDraw(canvas);
		
		// TODO Auto-generated method stub
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStyle(Style.FILL_AND_STROKE);

        Paint paintText = new Paint(Color.BLACK);
        paintText.setFakeBoldText(true);
		if (x != -1) {
			canvas.drawCircle(x, y, 11, paint);
			canvas.drawText("2", x-4, y+5, paintText);
			canvas_arrow(canvas, Math.round(x), Math.round(y), Math.round(this.getWidth()/2), Math.round(this.getHeight()/2));
		}
	}
	
	private void canvas_arrow(Canvas canvas, int fromx, int fromy, int tox, int toy){
	    int headlen = 10;   // length of head in pixels
	    double angle = Math.atan2(toy-fromy,tox-fromx);
	    Path path = new Path();
	    path.moveTo(fromx, fromy);
	    path.lineTo(tox, toy);
	    path.lineTo(new Float(tox-headlen*Math.cos(angle-Math.PI/6)), new Float(toy-headlen*Math.sin(angle-Math.PI/6)));
	    path.moveTo(tox, toy);
	    path.lineTo(new Float(tox-headlen*Math.cos(angle+Math.PI/6)), new Float(toy-headlen*Math.sin(angle+Math.PI/6)));
	    canvas.drawPath(path, new Paint(Color.BLACK));	    
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		x = event.getX();
		y = event.getY();
		
		invalidate();
		return super.onTouchEvent(event);
	}
}
